/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Vuong
 */
public class XMLVirtualDirectory extends XML{
    
    /*
    public static boolean add(VirtualDirectory newVFolder) throws IOException, JDOMException {
        try
        {
        newVFolder.setName("Ø" + newVFolder.getName());
        
        Document myDocument = (Document) readDocument(Constants.FILE_NAME_VIRTUAL_DIRECTORY);
        Element virtualFolder = myDocument.getRootElement();

        virtualFolder.addContent(createFileShare(newVFolder));
        outputDocumentToFile(myDocument, Constants.FILE_NAME_VIRTUAL_DIRECTORY);
        return true;
        }
        catch(Exception ex)
        {
            return false;
        }
    }
    
    private static Element createFileShare(VirtualDirectory newVFolder) {
        Element virtualFolder = new Element(newVFolder.getName());
        virtualFolder.addContent(new Element("Patch").addContent(newVFolder.getPatch()));
        return virtualFolder;
    }
    //Note: Ö Ø œ ð š Œ Ç... : cac ky tu co the lam the xml

    public static String GetPatchDirectory(String sNameVirtual) throws IOException, JDOMException {
        sNameVirtual = "Ø" + sNameVirtual;
        
        Document myDocument = readDocument(Constants.FILE_NAME_VIRTUAL_DIRECTORY);
        Element virtualFolders = myDocument.getRootElement();

        Element virtualFolder = virtualFolders.getChild(String.valueOf(sNameVirtual));

        if (virtualFolder != null) {           
            return virtualFolder.getChildText("Patch");
        } else {
            System.out.println("Can not find the patch Directory ");
        }
        return "";
    }
    
    public static boolean update(String sOldName, VirtualDirectory updateVFolder) throws IOException, JDOMException {
        try
        {
            sOldName = "Ø" + sOldName;
            updateVFolder.setName("Ø" + updateVFolder.getName());

            Document myDocument = readDocument(Constants.FILE_NAME_VIRTUAL_DIRECTORY);

            if (removeChildElement(myDocument, String.valueOf(sOldName))) {
                Element virtualFolder = myDocument.getRootElement();
                virtualFolder.addContent(createFileShare(updateVFolder));

                outputDocumentToFile(myDocument, Constants.FILE_NAME_VIRTUAL_DIRECTORY);

                return true;
            }
            return false;
        }
        catch(Exception ex)
        {
            return false;
        }
    }
    public static boolean delete(VirtualDirectory delVFolder) throws IOException, JDOMException {
        try
        {
            delVFolder.setName("Ø" + delVFolder.getName());

            Document myDocument = readDocument(Constants.FILE_NAME_VIRTUAL_DIRECTORY);

            if (removeChildElement(myDocument, String.valueOf(delVFolder.getName()))) {
                outputDocumentToFile(myDocument, Constants.FILE_NAME_VIRTUAL_DIRECTORY);
                return true;
            }
            return false;
        }
        catch(Exception ex)
        {
            return false;
        }
    }
    
    public static List<VirtualDirectory> GetALL() throws IOException, JDOMException {
        try
        {
            ArrayList<VirtualDirectory> srvFileInfos = new ArrayList<VirtualDirectory>();

            Document myDocument = readDocument(Constants.FILE_NAME_VIRTUAL_DIRECTORY);
            Element fileInfos = myDocument.getRootElement();

            List<Element> virFolderTags = fileInfos.getChildren();
            for (Element virFolderTag : virFolderTags) {
                VirtualDirectory virFolder = new VirtualDirectory(virFolderTag.getName().substring(1), virFolderTag.getChildText("Patch"));
                srvFileInfos.add(virFolder);
            }
            return srvFileInfos;
        }
        catch(Exception ex)
        {
            return null;
        }
    }
     * */
    
    
    
     public static boolean add(VirtualDirectory newVFolder) throws IOException, JDOMException {
        try
        {
        //newVFolder.setName("Ø" + newVFolder.getName());
        //newVFolder.setName("Key_" + System.currentTimeMillis());
        
        Document myDocument = (Document) readDocument(Constants.FILE_NAME_VIRTUAL_DIRECTORY);
        Element virtualFolder = myDocument.getRootElement();

        virtualFolder.addContent(createFileShare(newVFolder));
        outputDocumentToFile(myDocument, Constants.FILE_NAME_VIRTUAL_DIRECTORY);
        return true;
        }
        catch(Exception ex)
        {
            return false;
        }
    }
    
    private static Element createFileShare(VirtualDirectory newVFolder) {
        Element virtualFolder = new Element("Key_" + System.currentTimeMillis());
        
        virtualFolder.addContent(new Element("VirtualDirectory").addContent(newVFolder.getName()));
        virtualFolder.addContent(new Element("Patch").addContent(newVFolder.getPatch()));
        return virtualFolder;
    }
    //Note: Ö Ø œ ð š Œ Ç... : cac ky tu co the lam the xml

    public static String GetPatchDirectory(String sNameVirtual) throws IOException, JDOMException {
        //sNameVirtual = "Ø" + sNameVirtual;
        
        Document myDocument = readDocument(Constants.FILE_NAME_VIRTUAL_DIRECTORY);
        Element virtualFolders = myDocument.getRootElement();

        Element virtualFolder = virtualFolders.getChild(String.valueOf(sNameVirtual));

        if (virtualFolder != null) {           
            return virtualFolder.getChildText("Patch");
        } else {
            System.out.println("Can not find the patch Directory ");
        }
        return "";
    }
    
    public static boolean update(String sOldName, VirtualDirectory updateVFolder) throws IOException, JDOMException {
        try
        {
            Document myDocument = readDocument(Constants.FILE_NAME_VIRTUAL_DIRECTORY);
            Element fileInfos = myDocument.getRootElement();
            
            List<Element> virFolderTags = fileInfos.getChildren();
            for (Element virFolderTag : virFolderTags) {
                //VirtualDirectory virFolder = new VirtualDirectory(virFolderTag.getName().substring(1), virFolderTag.getChildText("Patch"));
                if(virFolderTag.getChildText("VirtualDirectory").equals(sOldName))
                {
                    if (removeChildElement(myDocument, virFolderTag.getName()))
                    {
                        Element virtualFolder = myDocument.getRootElement();
                        virtualFolder.addContent(createFileShare(updateVFolder));
                
                        outputDocumentToFile(myDocument, Constants.FILE_NAME_VIRTUAL_DIRECTORY);
                        return true;
                    }
                    return false;
                }
            }
            return false;
        }
        catch(Exception ex)
        {
            return false;
        }
    }
    public static boolean delete(VirtualDirectory delVFolder) throws IOException, JDOMException {
        try
        {
            Document myDocument = readDocument(Constants.FILE_NAME_VIRTUAL_DIRECTORY);
            Element fileInfos = myDocument.getRootElement();

            List<Element> virFolderTags = fileInfos.getChildren();
            for (Element virFolderTag : virFolderTags) {
                //VirtualDirectory virFolder = new VirtualDirectory(virFolderTag.getName().substring(1), virFolderTag.getChildText("Patch"));
                if(virFolderTag.getChildText("VirtualDirectory").equals(delVFolder.getName()))
                {
                    if (removeChildElement(myDocument, virFolderTag.getName())) 
                    {
                        outputDocumentToFile(myDocument, Constants.FILE_NAME_VIRTUAL_DIRECTORY);
                        return true;
                    }
                    return false;
                }
            }
            return false;
        }
        catch(Exception ex)
        {
            return false;
        }
    }
    
    public static List<VirtualDirectory> GetALL() throws IOException, JDOMException {
        try
        {
            ArrayList<VirtualDirectory> srvFileInfos = new ArrayList<VirtualDirectory>();

            Document myDocument = readDocument(Constants.FILE_NAME_VIRTUAL_DIRECTORY);
            Element fileInfos = myDocument.getRootElement();

            List<Element> virFolderTags = fileInfos.getChildren();
            for (Element virFolderTag : virFolderTags) {
                VirtualDirectory virFolder = new VirtualDirectory(virFolderTag.getChildText("VirtualDirectory"), virFolderTag.getChildText("Patch"));
                srvFileInfos.add(virFolder);
            }
            return srvFileInfos;
        }
        catch(Exception ex)
        {
            return null;
        }
    }
}
